package ThreadDemo;

/**
 * 死锁
 */
public class TreadDemo19 {
    //死锁1
    static class lock1{
        public void lock() {
            Object locker1 = new Object();
            Object locker2 = new Object();
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    synchronized (locker1) {
                        System.out.println("1");
                        synchronized (locker2) {
                            System.out.println("2");
                        }
                    }
                }
            });
            Thread thread1 = new Thread(new Runnable() {
                @Override
                public void run() {
                    synchronized (locker2) {
                        System.out.println("2");
                        synchronized (locker1) {
                            System.out.println(1);
                        }
                    }
                }
            });
            thread.start();
            thread1.start();
        }
    }
    //死锁2
    //当synchronized不是可重入锁时
    static class lock2{
        Object locker1=new Object();

        Thread thread=new Thread(new Runnable() {
            @Override
            synchronized public void run() {
                synchronized (locker1){
                    System.out.println("1");
                }
            }
        });
    }
    public static void main(String[] args) {


    }
}
